import { parse } from 'qs'
import { message } from 'antd'
import { isEmpty, cloneDeep } from 'lodash'
import config from '../../../../config/config'
import {
	updateStatus,
	batchDelete,
	batchUpdate,
	getOrganize,
	getDuty
} from './userListServ'

// 初始默认状态
const defaultState = {
	biz: {
		queryForm: {}
	},
	organizeList: [],
	dutyList: [],
  // 弹出框操作类型 -- delete：批量删除；forbid：批量禁用；open：批量启用
  modalType: '',

	//显示新增
  showAddVisible:false,
  // 弹出框显示状态
  showDetailModal: false,
  // 保存操作状态
  btnLoading: false,

    dataSource:[
        {
            key :"1",
            color:"2",
            size:"张三",
            checked:false,
        },
        {
            key :"2",
            color:"3",
            size:"lisi",
            checked:false,
        }
	]
};

const userListModel = {
	state: defaultState,
	//订阅数据
	subscriptions: {
		setup({ dispatch, history }) {
			return history.listen(location => {
				if (location.pathname == userListModel.pathname) {
					dispatch({ type: 'init', payload: {} })
				}
			})
		},
	},
	//数据逻辑部分
	effects: {
		*init({ payload }, { put, call, select }) {
			yield put({ type: 'getOrganize' });
			if (config.appId == 3) {
				yield put({ type: 'getDuty' });
			}
		},

		// 设置表单的值到状态机
		*setFormVal({ payload }, { put, call, select }) {
			let { biz } = yield select(d => d[userListModel.namespace])
			try {
				yield put({
					type: 'updateStore',
					payload: {
						biz: payload
					}
				})
			} catch (e) {
				console.error(e)
			}
		},

		//启用禁用
		*changeStatus({ payload }, { put, call, select }) {
			try {
				let { data, resultCode } = yield call(updateStatus, payload.params);
				if (resultCode == 0) {
					message.success('操作成功');
					payload.cb();
				}
			} catch (e) {
				console.error(e)
			}
		},

		//批量删除
		*batchDelete({ payload }, { put, call, select }) {
      yield put({ type: 'updateStore', payload: { btnLoading: true } });

			try {
				let { data, resultCode } = yield call(batchDelete, payload.params);
				if (resultCode == 0) {
					message.success('操作成功');
          yield put({ type: 'updateStore', payload: { showDetailModal: false } });
					payload.cb();
				}
			} catch (e) {
				console.error(e)
			}

      yield put({ type: 'updateStore', payload: { btnLoading: false } });
		},

		//批量更新状态
		*batchUpdate({ payload }, { put, call, select }) {
      yield put({ type: 'updateStore', payload: { btnLoading: true } });

			try {
				let { data, resultCode } = yield call(batchUpdate, payload.params);
				if (resultCode == 0) {
					message.success('操作成功');
          yield put({ type: 'updateStore', payload: { showDetailModal: false } });
					payload.cb();
				}
			} catch (e) {
				console.error(e)
			}

      yield put({ type: 'updateStore', payload: { btnLoading: false } });
		},

		*getOrganize({ payload }, { put, call, select }) {
			let state = yield select(e => e[userListModel.namespace]);
			try {
				let { data, resultCode } = yield call(getOrganize);
				if (resultCode == 0) {
					if (!data) { return }
					let organizeList = [];
					data.forEach((e, i) => {
						organizeList.push({ label: e.orgName, value: String(e.id) })
					})
					yield put({ type: 'updateStore', payload: { organizeList: organizeList } });
				}
			} catch (error) {
				console.error(error);
			}
		},

		*getDuty({ payload }, { put, call, select }) {
			let state = yield select(e => e[userListModel.namespace]);
			try {
				let { data, resultCode } = yield call(getDuty);
				if (resultCode == 0) {
					if (!data) { return }
					let dutyList = [];
					data.forEach((e, i) => {
						dutyList.push({ label: e.dutyName, value: String(e.id) })
					})
					yield put({ type: 'updateStore', payload: { dutyList: dutyList } });
				}
			} catch (error) {
				console.error(error);
			}
		},

	},
	//更新数据到state
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload);
		}
	}
}
export default userListModel
